// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const wxContext = cloud.getWXContext()
const db = cloud.database()

async function getCheck(event){
  const sendResult = await db.collection('stop').count()
  return sendResult
}
async function  onCheck(event){
  const sendResult = await db.collection('stop').add({
    data: {
      name: event.nickName
    }
  }) 
  return sendResult
}
async function  offCheck(event){
  const sendResult = await db.collection('stop').where({all:null}).remove()
  return sendResult
}
async function  resetCheck(event){
  const sendResult = await db.collection('stop').where({all:null}).remove()
  return sendResult
}

// 云函数入口函数
exports.main = async (event, context) => {
  switch (event.action) {
    case 'get': {
      return getCheck(event)
    }
    case 'on': {
      return onCheck(event)
    }
    case 'off': {
      return offCheck(event)
    }
    case 'reset': {
      return resetCheck(event)
    }
    default: {
      return
    }
  }
}